######### 调用示例 ###########
# sh run-hzero.sh hzero-register
# sh run-hzero.sh hzero-register/
#
# 自动打包和运行微服务工程
##############################

if [ -z "$1" ]; then
  echo "请输入HZERO服务名称："
  echo "example: sh run-hzero.sh hzero-register"
  exit 1
fi

SERVICE_NAME=${1,,} #参数转换成小写字符
SERVICE_NAME=${SERVICE_NAME%"/"} #删除末尾的"/"符
JAR=$SERVICE_NAME.jar
if [ $SERVICE_NAME == "hzero-register" ];then
  MPORT=8000
elif [ $SERVICE_NAME == "hzero-gateway" ];then
  MPORT=8080
elif [ $SERVICE_NAME == "hzero-oauth" ];then
  MPORT=8020
elif [ $SERVICE_NAME == "hzero-iam" ];then
  MPORT=8030
elif [ $SERVICE_NAME == "hzero-platform" ];then
  MPORT=8100
elif [ $SERVICE_NAME == "hzero-swagger" ];then
  MPORT=8050
elif [ $SERVICE_NAME == "hzero-config" ];then
  MPORT=8010
elif [ $SERVICE_NAME == "hzero-admin" ];then
  MPORT=8060
elif [ $SERVICE_NAME == "hzero-report" ];then
  MPORT=8210
elif [ $SERVICE_NAME == "hzero-scheduler" ];then
  MPORT=8130
elif [ $SERVICE_NAME == "hzero-message" ];then
  MPORT=8120
elif [ $SERVICE_NAME == "hzero-file" ];then
  MPORT=8110
elif [ $SERVICE_NAME == "hzero-import" ];then
  MPORT=8140
else
  MPORT=80
fi

echo ">>> git clone https://gitee.com/open-hand/$SERVICE_NAME.git"
git clone https://gitee.com/open-hand/$SERVICE_NAME.git

echo ">>> mvn clean package -Dmaven.javadoc.skip=true"
cd $SERVICE_NAME
mvn clean package -Dmaven.javadoc.skip=true -Dmaven.test.skip=true
echo ">>> cd target"
cd target
mv app.jar $JAR
echo ">>> kill -9 $(lsof -t -sTCP:LISTEN -i:$MPORT)"
kill -9 $(lsof -t -sTCP:LISTEN -i:$MPORT)
JAVA_OPTS="-Xms512m -Xmx1024m -Dspring.profiles.active=dev"
echo ">>> nohup java -jar $JAVA_OPTS $JAR >$1.log 2>&1 &"
BUILD_ID=dontKillMe nohup java -jar $JAVA_OPTS $JAR >../../$1.log 2>&1 &